package gepetto.reactor;

public abstract class TimeHandler extends AbstractEventHandler implements Comparable<TimeHandler> {
	private long timeout;
	private long lastHandle;
	
	public TimeHandler (long timeout_millis) {
		this.timeout = timeout_millis;
		reset ();
	}
	
	public void reset () {
		lastHandle = System.currentTimeMillis();
	}
	
	public long getTimeout () {
		return this.timeout;
	}
	
	public boolean handleEvent() {
		if ((System.currentTimeMillis () - lastHandle) >= timeout) {
			lastHandle += timeout;
			return handleTimeout();
		}
		return true;
	}
	
	public abstract boolean handleTimeout ();
	
	public int compareTo (TimeHandler time) {
		long diff = this.timeout - time.timeout;
		if (diff == 0) {
			return this.hashCode() - time.hashCode();
		}
		
		return (int) diff;
	}
}
